package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result<List<ShoppingCart>> list(Long userId) {
        return Result.success(shoppingCartMapper.list(userId));
    }

    @Override
    public void cleanShoppingCart(Long userId) {
        shoppingCartMapper.cleanShoppingCart(userId);
    }

    @Override
    public void addShopingCart(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart existShoppingCart = shoppingCartMapper.getShoppingCartByDishIdAndSetmealId(8L, dishId, setmealId);
        if (existShoppingCart != null) {
            // 修改 number 和 amount
            BigDecimal price = new BigDecimal(0);
            if (dishId != null) {
                Dish dish = dishMapper.getDishById(dishId);
                price = dish.getPrice();
            } else if (setmealId != null) {
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                price = setmeal.getPrice();
            }
            Long shoppingCartId = existShoppingCart.getId();
            Integer newNumber = existShoppingCart.getNumber() + 1;
            BigDecimal newAmount = existShoppingCart.getAmount().add(price);
            shoppingCartMapper.updateShoppingCart(shoppingCartId, newNumber, newAmount);
        } else {
            // 新增
            ShoppingCart shoppingCart = new ShoppingCart();
            if (dishId != null) {
                shoppingCart.setDishId(dishId);
                Dish dish = dishMapper.getDishById(dishId);
                BigDecimal dishPrice = dish.getPrice();
                String dishName = dish.getName();
                String dishImage = dish.getImage();
                shoppingCart.setDishFlavor(dishFlavor);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
                shoppingCart.setDishId(dishId);
                shoppingCart.setImage(dishImage);
                shoppingCart.setName(dishName);
                shoppingCart.setAmount((shoppingCart.getAmount() == null ? BigDecimal.ZERO : shoppingCart.getAmount()).add(dishPrice));
            } else {
                shoppingCart.setSetmealId(setmealId);
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                BigDecimal setmealPrice = setmeal.getPrice();
                String setmealName = setmeal.getName();
                String setmealImage = setmeal.getImage();
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
                shoppingCart.setSetmealId(setmealId);
                shoppingCart.setImage(setmealImage);
                shoppingCart.setName(setmealName);
                shoppingCart.setAmount((shoppingCart.getAmount() == null ? BigDecimal.ZERO : shoppingCart.getAmount()).add(setmealPrice));
            }
            shoppingCart.setUserId(8L);
            shoppingCartMapper.save(shoppingCart);
        }
    }
}
